import os
import tkinter as tk
from tkinter import filedialog
from tkinter import ttk
import threading
import client
import time

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.grid(padx=10, pady=10)
        self.create_widgets()

    def create_widgets(self):
        self.master.columnconfigure(0,weight=1)
        self.master.rowconfigure(0,weight=1)
        self.ip_label = tk.Label(self, text="IP Address:")
        self.ip_label.grid(row=0, column=0,padx=5,pady=5)

        self.ip_entry = tk.Entry(self)
        self.ip_entry.grid(row=0, column=1,padx=5,pady=5)
        
        self.file_label = tk.Label(self, text="No file selected")
        self.file_label.grid(row=1, column=0,padx=5,pady=5)

        self.file_button = tk.Button(self)
        self.file_button["text"] = "Choose File"
        self.file_button["command"] = self.load_file
        self.file_button.grid(row=1, column=1 ,padx=5,pady=5)

        self.start_button = tk.Button(self)
        self.start_button["text"] = "Start Transfer"
        self.start_button["command"] = self.start_transfer
        self.start_button.grid(row=2, column=0, columnspan=2,padx=5,pady=5)

        self.speed_label = tk.Label(self, text="Transfer Speed:")
        self.speed_label.grid(row=3, column=0,padx=5,pady=5)
        
        self.speed_value_label = tk.Label(self, text="0 KB/s")
        self.speed_value_label.grid(row=3, column=1,padx=5,pady=5)

        self.progress_label = tk.Label(self, text="Transfer Progress:")
        self.progress_label.grid(row=4, column=0,padx=5,pady=5)

        self.progress_bar = ttk.Progressbar(self)
        self.progress_bar.grid(row=4, column=1,padx=5,pady=5)
        self.progress_bar['value'] = 0
        

    def load_file(self):
        self.filename = filedialog.askopenfilename(initialdir="/", title="Select file")
        self.file_label["text"] = self.filename.split('/')[-1]
        client.FILE_NAME = self.filename
        self.progress_bar['maximum'] = os.stat(self.filename).st_size

        
    def update_speed(self):
        while True:
            self.speed_value_label["text"]="{:.2f} KB/s".format(client.speed)
            self.progress_bar['value'] = client.sended_bytes
            time.sleep(1)
            if self.speed_value_label["text"]=="{:.2f} KB/s".format(client.speed):
                self.progress_bar['value'] = client.file_size
                tk.messagebox.showinfo("Transfer Complete", "The file transfer has been completed successfully!")
                break

    def start_transfer(self):
        ip_address = self.ip_entry.get()
        # Start a new thread to handle the file transfer
        transfer_thread = threading.Thread(target=client.main)
        transfer_thread.start()
        update_speed_thread = threading.Thread(target=self.update_speed)
        update_speed_thread.start()
        


root = tk.Tk()
app = Application(master=root)
app.mainloop()
